package com.ict.ms.realTime.service.api.impl;

import com.alibaba.fastjson.JSONObject;
import com.ict.ms.realTime.facade.api.IStaticInfoService;
import com.ict.ms.realTime.facade.dto.ResultObj;
import com.ict.ms.realTime.facade.dto.ShipBaseInfoParam;
import com.ict.ms.realTime.facade.model.Aggregated_AIS_Ship_Type;
import com.ict.ms.realTime.facade.model.StaticCountry;
import com.ict.ms.realTime.service.dao.IStaticInfoDao;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StaticInfoService  implements   IStaticInfoService {
    @Resource
    IStaticInfoDao iStaticInfoDao;

    /**
     * 获取所有国家地区信息
     * @return
     */
    public ResultObj getListStaticCountry(){
        ResultObj resultObj=new ResultObj();
        try{
            List<StaticCountry> staticCountryList = iStaticInfoDao.getListStaticCountry();
            if (staticCountryList.size()<1){
                resultObj.setState(0);
            }else {
                resultObj.setState(1);
                resultObj.setObj(staticCountryList);
            }
        }catch (Exception e){
            System.out.println("数据库操作失败："+e);
            resultObj.setState(-1);
        }

        return resultObj;
    }

    /**
     * 获取所有船舶类型列表
     * @return
     */
    public ResultObj getListStaticShipType(){
        ResultObj resultObj=new ResultObj();
        try{
            List<Aggregated_AIS_Ship_Type> listShipType = iStaticInfoDao.getListShipType();
            if (listShipType.size()<1){
                resultObj.setState(0);
            }else {
                resultObj.setState(1);
                resultObj.setObj(listShipType);
            }
        }catch (Exception e){
            System.out.println("数据库操作失败："+e);
            resultObj.setState(-1);
        }

        return resultObj;
    }

    /**
     * 修改船舶档案资料
     * @return
     */
    public ResultObj updateCivilShipInfo(int shipid,int shiptype,String civilStr){
        ResultObj resultObj=new ResultObj();
        try {
            JSONObject obj = JSONObject.parseObject(civilStr);
            ShipBaseInfoParam shipBaseInfo = new ShipBaseInfoParam();

            shipBaseInfo.setTarget_id_orig(shipid);
            shipBaseInfo.setOrig_info_type(shiptype);

            if (obj.containsKey("areacode")){
                shipBaseInfo.setAreacode(obj.getString("areacode"));
            }if (obj.containsKey("province")){
                shipBaseInfo.setProvince(obj.getString("province"));
            }if (obj.containsKey("city")){
                shipBaseInfo.setCity(obj.getString("city"));
            }if (obj.containsKey("country")){
                shipBaseInfo.setCountry(obj.getString("country"));
            }if (obj.containsKey("ship_type")){
                shipBaseInfo.setShip_type(obj.getString("ship_type"));
            }if (obj.containsKey("initregister")){
                shipBaseInfo.setInitregister(obj.getString("initregister"));
            }if (obj.containsKey("callsign")){
                shipBaseInfo.setCallsign(obj.getString("callsign"));
            }if (obj.containsKey("mmsi")){
                shipBaseInfo.setMmsi(obj.getInteger("mmsi"));
            }if (obj.containsKey("imo")){
                shipBaseInfo.setImo(obj.getInteger("imo"));
            }if (obj.containsKey("classification")){
                shipBaseInfo.setClassification(obj.getString("classification"));
            }

            if (obj.containsKey("beidouID")){
                shipBaseInfo.setBeidouID(obj.getInteger("beidouID"));
            }if (obj.containsKey("flag")){
                shipBaseInfo.setFlag(obj.getString("flag"));
            }if (obj.containsKey("old_name")){
                shipBaseInfo.setOld_name(obj.getString("old_name"));
            }if (obj.containsKey("old_flag")){
                shipBaseInfo.setOld_flag(obj.getString("old_flag"));
            }if (obj.containsKey("ascription")){
                shipBaseInfo.setAscription(obj.getString("ascription"));
            }if (obj.containsKey("satellite_communication")){
                shipBaseInfo.setSatellite_communication(obj.getString("satellite_communication"));
            }if (obj.containsKey("fax")){
                shipBaseInfo.setFax(obj.getString("fax"));
            }if (obj.containsKey("contacttelenight")){
                shipBaseInfo.setContacttelenight(obj.getString("contacttelenight"));
            }if (obj.containsKey("audit_time")){
                shipBaseInfo.setAudit_time(obj.getString("audit_time"));
            }if (obj.containsKey("post")){
                shipBaseInfo.setPost(obj.getString("post"));
            }

            if (obj.containsKey("logouttime")){
                shipBaseInfo.setLogouttime(obj.getString("logouttime"));
            }if (obj.containsKey("device_sn")){
                shipBaseInfo.setDevice_sn(obj.getString("device_sn"));
            }if (obj.containsKey("isagent")){
                shipBaseInfo.setIsagent(obj.getString("isagent"));
            }if (obj.containsKey("aissn")){
                shipBaseInfo.setAissn(obj.getString("aissn"));
            }if (obj.containsKey("remark")){
                shipBaseInfo.setRemark(obj.getString("remark"));
            }if (obj.containsKey("state")){
                shipBaseInfo.setState(obj.getString("state"));
            }if (obj.containsKey("ship_name")){
                shipBaseInfo.setShip_name(obj.getString("ship_name"));
            }if (obj.containsKey("ship_english_name")){
                shipBaseInfo.setShip_english_name(obj.getString("ship_english_name"));
            }if (obj.containsKey("fishing_boats_coding")){
                shipBaseInfo.setFishing_boats_coding(obj.getString("fishing_boats_coding"));
            }if (obj.containsKey("fishing_boat_belongs")){
                shipBaseInfo.setFishing_boat_belongs(obj.getString("fishing_boat_belongs"));
            }

            if (obj.containsKey("registry")){
                shipBaseInfo.setRegistry(obj.getString("registry"));
            }if (obj.containsKey("registryE")){
                shipBaseInfo.setRegistryE(obj.getString("registryE"));
            }if (obj.containsKey("shipyardn")){
                shipBaseInfo.setShipyardn(obj.getString("shipyardn"));
            }if (obj.containsKey("shipyardnE")){
                shipBaseInfo.setShipyardnE(obj.getString("shipyardnE"));
            }if (obj.containsKey("shipyard_address")){
                shipBaseInfo.setShipyard_address(obj.getString("shipyard_address"));
            }if (obj.containsKey("shipyard_address_english")){
                shipBaseInfo.setShipyard_address_english(obj.getString("shipyard_address_english"));
            }if (obj.containsKey("ship_length")){
                shipBaseInfo.setShip_length(obj.getDouble("ship_length"));
            }if (obj.containsKey("ship_width")){
                shipBaseInfo.setShip_width(obj.getDouble("ship_width"));
            }if (obj.containsKey("moulded_depth")){
                shipBaseInfo.setMoulded_depth(obj.getDouble("moulded_depth"));
            }if (obj.containsKey("total_tonnage")){
                shipBaseInfo.setTotal_tonnage(obj.getDouble("total_tonnage"));
            }

            if (obj.containsKey("net_tonnage")){
                shipBaseInfo.setNet_tonnage(obj.getInteger("net_tonnage"));
            }if (obj.containsKey("host_total_power")){
                shipBaseInfo.setHost_total_power(obj.getInteger("host_total_power"));
            }if (obj.containsKey("host_type1")){
                shipBaseInfo.setHost_type1(obj.getString("host_type1"));
            }if (obj.containsKey("host_type2")){
                shipBaseInfo.setHost_type2(obj.getString("host_type2"));
            }if (obj.containsKey("host_type3")){
                shipBaseInfo.setHost_type3(obj.getString("host_type3"));
            }if (obj.containsKey("host_power1")){
                shipBaseInfo.setHost_power1(obj.getDouble("host_power1"));
            }if (obj.containsKey("host_power2")){
                shipBaseInfo.setHost_power2(obj.getDouble("host_power2"));
            }if (obj.containsKey("host_power3")){
                shipBaseInfo.setHost_power3(obj.getDouble("host_power3"));
            }if (obj.containsKey("hull_material")){
                shipBaseInfo.setHull_material(obj.getString("hull_material"));
            }if (obj.containsKey("completion_time")){
                shipBaseInfo.setCompletion_time(obj.getString("completion_time"));
            }

            if (obj.containsKey("number_of_ship_host")){
                shipBaseInfo.setNumber_of_ship_host(obj.getString("number_of_ship_host"));
            }if (obj.containsKey("model_of_ship_host")){
                shipBaseInfo.setModel_of_ship_host(obj.getString("model_of_ship_host"));
            }if (obj.containsKey("name_of_sub_vessel")){
                shipBaseInfo.setName_of_sub_vessel(obj.getString("name_of_sub_vessel"));
            }if (obj.containsKey("number_of_ship")){
                shipBaseInfo.setNumber_of_ship(obj.getString("number_of_ship"));
            }if (obj.containsKey("total_power_of_sub_ship")){
                shipBaseInfo.setTotal_power_of_sub_ship(obj.getDouble("total_power_of_sub_ship"));
            }if (obj.containsKey("job_type")){
                shipBaseInfo.setJob_type(obj.getString("job_type"));
            }if (obj.containsKey("operation_mode")){
                shipBaseInfo.setOperation_mode(obj.getString("operation_mode"));
            }if (obj.containsKey("job_type_two")){
                shipBaseInfo.setJob_type_two(obj.getString("job_type_two"));
            }if (obj.containsKey("operation_mode_two")){
                shipBaseInfo.setOperation_mode_two(obj.getString("operation_mode_two"));
            }if (obj.containsKey("auxiliary_operation_mode")){
                shipBaseInfo.setAuxiliary_operation_mode(obj.getString("auxiliary_operation_mode"));
            }

            if (obj.containsKey("workplace")){
                shipBaseInfo.setWorkplace(obj.getString("workplace"));
            }if (obj.containsKey("fishing_name")){
                shipBaseInfo.setFishing_name(obj.getString("fishing_name"));
            }if (obj.containsKey("specifications")){
                shipBaseInfo.setSpecifications(obj.getString("specifications"));
            }if (obj.containsKey("quantity")){
                shipBaseInfo.setQuantity(obj.getString("quantity"));
            }if (obj.containsKey("fishing_species")){
                shipBaseInfo.setFishing_species(obj.getString("fishing_species"));
            }if (obj.containsKey("fishing_quota")){
                shipBaseInfo.setFishing_quota(obj.getDouble("fishing_quota"));
            }if (obj.containsKey("start_time")){
                shipBaseInfo.setStart_time(obj.getString("start_time"));
            }if (obj.containsKey("end_time")){
                shipBaseInfo.setEnd_time(obj.getString("end_time"));
            }if (obj.containsKey("production_type")){
                shipBaseInfo.setProduction_type(obj.getString("production_type"));
            }if (obj.containsKey("specific_workplace")){
                shipBaseInfo.setSpecific_workplace(obj.getString("specific_workplace"));
            }

            if (obj.containsKey("icoe_number")){
                shipBaseInfo.setIcoe_number(obj.getString("icoe_number"));
            }if (obj.containsKey("holder")){
                shipBaseInfo.setHolder(obj.getString("holder"));
            }if (obj.containsKey("holder_card")){
                shipBaseInfo.setHolder_card(obj.getString("holder_card"));
            }if (obj.containsKey("address")){
                shipBaseInfo.setAddress(obj.getString("address"));
            }if (obj.containsKey("witness_call")){
                shipBaseInfo.setWitness_call(obj.getString("witness_call"));
            }if (obj.containsKey("postal_code")){
                shipBaseInfo.setPostal_code(obj.getString("postal_code"));
            }if (obj.containsKey("owner_name")){
                shipBaseInfo.setOwner_name(obj.getString("owner_name"));
            }if (obj.containsKey("owner_ename")){
                shipBaseInfo.setOwner_ename(obj.getString("owner_ename"));
            }if (obj.containsKey("owner_address")){
                shipBaseInfo.setOwner_address(obj.getString("owner_address"));
            }if (obj.containsKey("owner_addresse")){
                shipBaseInfo.setOwner_addresse(obj.getString("owner_addresse"));
            }

            if (obj.containsKey("owner_phone")){
                shipBaseInfo.setOwner_phone(obj.getString("owner_phone"));
            }if (obj.containsKey("lssuer")){
                shipBaseInfo.setLssuer(obj.getString("lssuer"));
            }if (obj.containsKey("date_of_issue")){
                shipBaseInfo.setDate_of_issue(obj.getString("date_of_issue"));
            }if (obj.containsKey("expired_time")){
                shipBaseInfo.setExpired_time(obj.getString("expired_time"));
            }if (obj.containsKey("shipowner")){
                shipBaseInfo.setShipowner(obj.getString("shipowner"));
            }if (obj.containsKey("nationality")){
                shipBaseInfo.setNationality(obj.getString("nationality"));
            }if (obj.containsKey("registered_owner")){
                shipBaseInfo.setRegistered_owner(obj.getString("registered_owner"));
            }if (obj.containsKey("users")){
                shipBaseInfo.setUsers(obj.getString("users"));
            }if (obj.containsKey("user_nationality")){
                shipBaseInfo.setUser_nationality(obj.getString("user_nationality"));
            }if (obj.containsKey("carrier")){
                shipBaseInfo.setCarrier(obj.getString("carrier"));
            }

            if (obj.containsKey("carrier_nationality")){
                shipBaseInfo.setCarrier_nationality(obj.getString("carrier_nationality"));
            }if (obj.containsKey("update_time")){
                shipBaseInfo.setUpdate_time(obj.getString("update_time"));
            }if (obj.containsKey("shipowner_cn")){
                shipBaseInfo.setShipowner_cn(obj.getString("shipowner_cn"));
            }if (obj.containsKey("mailing_address")){
                shipBaseInfo.setMailing_address(obj.getString("mailing_address"));
            }if (obj.containsKey("information")){
                shipBaseInfo.setInformation(obj.getString("information"));
            }if (obj.containsKey("port_entry")){
                shipBaseInfo.setPort_entry(obj.getString("port_entry"));
            }if (obj.containsKey("hongKMacao")){
                shipBaseInfo.setHongKMacao(obj.getString("hongKMacao"));
            }if (obj.containsKey("postcode")){
                shipBaseInfo.setPostcode(obj.getString("postcode"));
            }if (obj.containsKey("fishing_lvp")){
                shipBaseInfo.setFishing_lvp(obj.getString("fishing_lvp"));
            }if (obj.containsKey("fishing_inspection")){
                shipBaseInfo.setFishing_inspection(obj.getString("fishing_inspection"));
            }

            if (obj.containsKey("nationality_registration")){
                shipBaseInfo.setNationality_registration(obj.getString("nationality_registration"));
            }if (obj.containsKey("completion_date")){
                shipBaseInfo.setCompletion_date(obj.getString("completion_date"));
            }if (obj.containsKey("ship_icn")){
                shipBaseInfo.setShip_icn(obj.getString("ship_icn"));
            }if (obj.containsKey("validity_ovic")){
                shipBaseInfo.setValidity_ovic(obj.getString("validity_ovic"));
            }if (obj.containsKey("title_cn")){
                shipBaseInfo.setTitle_cn(obj.getString("title_cn"));
            }if (obj.containsKey("date_oaoo")){
                shipBaseInfo.setDate_oaoo(obj.getString("date_oaoo"));
            }if (obj.containsKey("registration_cn")){
                shipBaseInfo.setRegistration_cn(obj.getString("registration_cn"));
            }if (obj.containsKey("validity_orc")){
                shipBaseInfo.setValidity_orc(obj.getString("validity_orc"));
            }if (obj.containsKey("cancellation_number")){
                shipBaseInfo.setCancellation_number(obj.getString("cancellation_number"));
            }if (obj.containsKey("vessel_nia")){
                shipBaseInfo.setVessel_nia(obj.getString("vessel_nia"));
            }

            if (obj.containsKey("approval_povni")){
                shipBaseInfo.setApproval_povni(obj.getString("approval_povni"));
            }if (obj.containsKey("fishing_number")){
                shipBaseInfo.setFishing_number(obj.getString("fishing_number"));
            }

            if (iStaticInfoDao.countIfExist(shipBaseInfo)>1){
                iStaticInfoDao.updateCivilShipInfo(shipBaseInfo);
            }else if (iStaticInfoDao.countIfExist(shipBaseInfo)==0){
                iStaticInfoDao.insertCivilShipInfo(shipBaseInfo);
            }
            resultObj.setState(1);
        }catch (Exception e){
            System.out.println("数据库操作失败："+e);
            resultObj.setState(-1);
        }

        return resultObj;
    }
}